package fms.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * Disc: 本类用于对航空业常见的对象进行字符校验
 */
public class ValidCheckAviation {
    // 航班机尾号校验
    private static final String regexAircraftId = "[\\s\\S]{6,7}";
    // 风速正则校验
    private static final String regexWindVel = "\\d{3}";
    // 风向正则校验
    private static final String regexWindDir = "\\d{2}";
    // 风温正则校验
    private static final String regexWindTemp = "[MP ]\\d{2}";
    // 经度正则校验
    private static final String regexWindLon = "[EW]\\d{6}";
    // 纬度正则校验
    private static final String regexWindLat = "[NS]\\d{5}";
    // 620格式报文的正文合法性校验
    private static String regexAcarsFreeText = "";
    //MqUtil.getConf("/config/regexValidCheck.properties").getProperty("regexAcarsFreeText");

    public static enum CheckType {
        AircraftId, // 0
        WindVel,    // 1
        WindDir,    // 2
        WindTemp,   // 3
        WindLon,    // 4
        WindLat,    // 5
        AcarsFreeText // 6
    }

    /**
     * @param checkStr:    待检验的字符串
     * @param checkType:   待校验的字符串类型，不同类型对应不同的正则匹配串
     * @param assignRegex: 自定义正则
     * @return
     */
    public static boolean RegexCheck(String checkStr, int checkType, String assignRegex) {
        if (null != checkStr && !checkStr.equals("")) {
            Pattern pattern = null;
            if (null == assignRegex) {
                switch (checkType) {
                    case 0:
                        pattern = pattern.compile(regexAircraftId);
                        break;
                    case 1:
                        pattern = pattern.compile(regexWindVel);
                        break;
                    case 2:
                        pattern = pattern.compile(regexWindDir);
                        break;
                    case 3:
                        pattern = pattern.compile(regexWindTemp);
                        break;
                    case 4:
                        pattern = pattern.compile(regexWindLon);
                        break;
                    case 5:
                        pattern = pattern.compile(regexWindLat);
                        break;
                    case 6:
                        pattern = pattern.compile(regexAcarsFreeText);
                        break;
                }
            } else {
                pattern = pattern.compile(assignRegex);
            }
            Matcher matcher = pattern.matcher(checkStr);
            if (matcher.matches()) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public static boolean RegexCheck(String checkStr, int checkType) {
        return RegexCheck(checkStr, checkType, null);
    }

    public static void main(String[] args) {
//        System.out.println(RegexCheck("11311", CheckType.AcarsFreeText.ordinal()));
        System.out.println(RegexCheck(" 11", CheckType.WindTemp.ordinal()));
//        System.out.println(regexAcarsFreeText);
    }

}
